//
//  SceneDelegate.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/2/27.
//

import UIKit
import IQKeyboardManagerSwift
import BaiduMapAPI_Base
import BMKLocationKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else {
            return
        }
        
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.resignOnTouchOutside = true
        
        let manager = BMKMapManager()
        if !manager.start(mapAK, generalDelegate: self) {
            print("百度地图引擎启动失败")
        }
        BMKMapManager.setAgreePrivacy(true)
        BMKLocationAuth.sharedInstance().checkPermision(withKey: mapAK, authDelegate: self)
        BMKLocationAuth.sharedInstance().setAgreePrivacy(true)
        
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
        }
        
        window = UIWindow(windowScene: scene)
        
        if MYGUserManager.isLogin {
            let vc = MYGTabBarController()
            window?.rootViewController = vc
            MYGSocketManager.shared.connect()
            let isSuc = MYGUserManager.isDoctorInfComplete
            MYGUserManager.isDoctorInfComplete = isSuc
        } else {
            let vc = MYGNavigationController(rootViewController: MYGRegisterController())
            window?.rootViewController = vc
        }

        window?.makeKeyAndVisible()
        
      
    }
    //var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

//    func sceneWillEnterForeground(_ scene: UIScene) {
//        // Called as the scene transitions from the background to the foreground.
//        // Use this method to undo the changes made on entering the background.
//        // 当应用程序回到前台时，结束后台任务
//        endBackgroundTask()
//        if MYGUserManager.isLogin {
//            MYGSocketManager.shared.reContent()
//        }
//    }
//
//    func sceneDidEnterBackground(_ scene: UIScene) {
//        // Called as the scene transitions from the foreground to the background.
//        // Use this method to save data, release shared resources, and store enough scene-specific state information
//        // to restore the scene back to its current state.
//        backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
//            // 如果在10分钟内没有完成任务，系统会终止应用程序
//            self.endBackgroundTask()
//        })
//        
//        // 在这里可以添加你的WebSocket保活逻辑
//        // 比如重连WebSocket，或者保持心跳
//        
//    }
//    func endBackgroundTask() {
//        if backgroundTask != UIBackgroundTaskIdentifier.invalid {
//            UIApplication.shared.endBackgroundTask(backgroundTask)
//            backgroundTask = UIBackgroundTaskIdentifier.invalid
//        }
//    }


}

extension SceneDelegate: BMKGeneralDelegate, BMKLocationAuthDelegate {
    
}

